<?php
require_once ('./Model/LoginHandler.php');
require_once ('./View/LoginView.php');
require_once ('./Model/DBSettings.php');
require_once ('./Static/Messages.php');
require_once ('./Static/UserStatus.php');

 
class LoginController {
	
	public static $loginStatus = UserStatus::LOGGED_OUT;
	
	private $username = null;
	private $loginHandler = null;
	private $loginView = null;
	
	private $tokenArrayValidKey = 'valid';
	private $tokenArrayUsernameKey = 'username';
	
	/*
	 * Kontrollerar och loggar in och ut användare
	 * Visar relevanta meddelanden
	 * 
	 * @retun string HTML
	 */
	public function DoControll(){
		
		$db = new mysqli(DBSettings::$host, DBSettings::$user, DBSettings::$password, DBSettings::$database);
		
		$this->loginHandler = new LoginHandler($db);
		$this->loginView = new LoginView();
		
		
		switch($this->IsLoggedIn()){
			case UserStatus::LOGGEDIN_COOKIE:
				$newToken = $this->loginHandler->GenerateToken($this->username);
				$this->loginView->RememberMe($newToken);
				self::$loginStatus = UserStatus::LOGGED_IN;
				return $this->loginView->DoLoginBox($this->username, true);
				break;
				
			case UserStatus::LOGGEDIN_SESSION:
				self::$loginStatus = UserStatus::LOGGED_IN;
				return $this->loginView->DoLoginBox($this->username, true);
				
			case UserStatus::COOKIE_ERROR:
				self::$loginStatus = UserStatus::LOGGED_OUT;
				return $this->loginView->DoLoginBox(Messages::ERROR_USERTOKEN_INVALID, false);
				
			case UserStatus::COOKIE_HIJACK_ERROR:
				$this->loginView->RemoveRemember();
				self::$loginStatus = UserStatus::LOGGED_OUT;
				return $this->loginView->DoLoginBox(Messages::ERROR_USERTOKEN_HIJACKED, false);
				
			case UserStatus::LOGIN_ERROR:
				self::$loginStatus = UserStatus::LOGGED_OUT;
				return $this->loginView->DoLoginBox(Messages::ERROR_USERINFO_INVALID, false);
			
			case UserStatus::DO_LOGIN:
				self::$loginStatus = UserStatus::LOGGED_IN;
				return $this->loginView->DoLoginBox($this->username, true);
				
			case UserStatus::DO_LOGIN_COOKIE:
				self::$loginStatus = UserStatus::LOGGED_IN;
				$this->loginView->RememberMe($this->loginHandler->GenerateToken($this->loginView->GetUserName()));
				return $this->loginView->DoLoginBox($this->username, true);
				
			case UserStatus::DO_LOGOUT:
				$this->loginHandler->RemoveToken($this->loginView->GetRememberToken());
				$this->loginHandler->DoLogout();
				$this->loginView->RemoveRemember();
				self::$loginStatus = UserStatus::LOGGED_OUT;
				return $this->loginView->DoLoginBox();	
				
			default:
				self::$loginStatus = UserStatus::LOGGED_OUT;
				return $this->loginView->DoLoginBox();	
		}	
	}
	//returnerar aktuell status för användaren samt eventuella fel som uppstått
	public function IsLoggedIn(){

		
		if($this->loginView->TriedToLogOut() == true){
			return UserStatus::DO_LOGOUT;
		}
		
		if(($token = $this->loginView->GetRememberToken()) != null){
			if(($tokenIsValid = $this->loginHandler->IsValidToken($token)) != null){
				if($tokenIsValid){
					$this->username = LoginHandler::$username;
					$newToken = $this->loginHandler->GenerateToken($this->username);
					$this->loginView->RememberMe($newToken);	
					
					return UserStatus::LOGGEDIN_COOKIE;
				}
				elseif(!$tokenIsValid){
					return UserStatus::COOKIE_HIJACK_ERROR;	
				}
				return UserStatus::COOKIE_ERROR;
			}
				
		}
		
		if($this->loginHandler->IsLoggedIn($this->loginHandler->GetSessionUsername(), $this->loginHandler->GetSessionPassword()) == true){
			$this->username = $this->loginHandler->GetSessionUsername();
			return UserStatus::LOGGEDIN_SESSION;
		}
		
		if($this->loginView->TriedToLogIn() == true){
			if($this->loginHandler->DoLogin($this->loginView->GetUserName(), $this->loginView->GetPassword()) == true){
				
				$this->username = $this->loginView->GetUserName();
				if($this->loginView->IsRemember() == true){
					return UserStatus::DO_LOGIN_COOKIE;
				}	
				return UserStatus::DO_LOGIN;	
			}
			else{
				return UserStatus::LOGIN_ERROR;
			}				
		}
		
		return UserStatus::NOT_LOGGEDIN;
		
	}
	//returnerar användarid för aktuell användare
	public function GetUserId(){
		return $this->loginHandler->GetUserId($this->username);
	}
}
?>





















